package com.imay.live.view.custom;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.imay.live.R;
import com.imay.live.utils.ViewUtil;

/**
 * Created by nick on 2017/6/28.
 * 超胆播30s倒计时
 */

public class CountdownView extends LinearLayout {
    private int [] imageArr = {R.mipmap.number0,R.mipmap.number1,R.mipmap.number2,R.mipmap.number3,R.mipmap.number4,R.mipmap.number5,R.mipmap.number6,R.mipmap.number7,R.mipmap.number8,R.mipmap.number9};
    private ImageView iv_decade_countdown;
    private ImageView iv_unit_countdown;
    private ShortLiveTimer shortLiveTimer;
    private CountDownTimerListener mListener;

    public CountdownView(Context context) {
        super(context);
    }

    public CountdownView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CountdownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        inflate(context, R.layout.view_count_down,this);
        iv_decade_countdown = ViewUtil.findView(this,R.id.iv_decade_countdown);
        iv_unit_countdown = ViewUtil.findView(this,R.id.iv_unit_countdown);
    }

    public void startCountDown(long millisInFuture, CountDownTimerListener listener){
        setVisibility(VISIBLE);
        mListener = listener;
        if (shortLiveTimer == null){
            shortLiveTimer = new ShortLiveTimer(millisInFuture, 1000);
        }
        shortLiveTimer.start();
    }

    public void cancelCountDown() {
        if (shortLiveTimer != null) {
            shortLiveTimer.cancel();
        }
        setVisibility(GONE);
    }

    public void setTime(long millis) {
        if (millis <= 0) {
            setVisibility(GONE);
        } else {
            if (getVisibility() != VISIBLE) {
                setVisibility(VISIBLE);
            }
            iv_decade_countdown.setBackgroundResource(getDecadeImageId((int) (millis/1000)));
            iv_unit_countdown.setBackgroundResource(getUnitImageId((int) (millis/1000)));
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cancelCountDown();
    }

    private class ShortLiveTimer extends CountDownTimer {

        public ShortLiveTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            setTime(millisUntilFinished);
            if (mListener != null) {
                mListener.onTick(millisUntilFinished);
            }
        }

        @Override
        public void onFinish() {
            setVisibility(GONE);
            if (mListener != null) {
                mListener.onFinish();
            }
        }
    }

    private int getDecadeImageId(int time){
        int decade = time/10;
//        if (decade==0){
//            return 0;
//        }
        return imageArr[decade];
    }
    private int getUnitImageId(int time){
        int decade = time%10;
        return imageArr[decade];
    }

    public interface CountDownTimerListener {
        void onTick(long millisUntilFinished);
        void onFinish();
    }
}
